package com.io;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamClass {
    public static void main(String[] args) {
        try {
            InputStream inputStream = new FileInputStream("/home/mls/data/test/text.doc");
            int n;
            while ((n = inputStream.read()) != -1) {
                System.out.println(n);
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte[] data = { 72, 101, 108, 108, 111, 33 };
        try (InputStream input = new ByteArrayInputStream(data)) {
            String s = readAsString(input);
            System.out.println(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readAsString(InputStream inputStream) throws IOException {
        int n;
        StringBuilder stringBuilder = new StringBuilder();
        while ((n = inputStream.read()) != -1) {
            stringBuilder.append(n);
        }
        return stringBuilder.toString();
    }
}
